<?php
/**
 * Created By PhpStorm
 * Author: patrick wu
 * Date: 2020/12/1
 * Time: 15:59
 */
namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Service\Admin\OwnerService;
use App\Constant\ErrorConstant;

class OwnerController extends BaseController
{
    protected $service;

    public function __construct(Request $request, OwnerService $service)
    {
        parent::__construct($request);

        $this->service  =   $service;
    }

    /**
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function listOwner()
    {
        $validator  =   Validator::make($this->request->post(), [
            'query'  =>  'array'
        ], [
            'query.array' =>  '盒子用户搜索条件必须为数组',
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_OWNER_LIST_ERROR, $validator->errors()->first());
        }

        $query  =   $this->request->post('query', []);
        $limit  =   (int) $this->request->input('page_size', NULL);
        $page   =   (int) $this->request->input('page', 1);
        [$flag, $result]    =   $this->service->ownerList($query, $limit, $page);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_OWNER_LIST_ERROR, $result);
        }
    }

    /**
     * 添加盒子用户
     * @return \Illuminate\Http\JsonResponse
     */
    public function addOwner()
    {
        $validator  =   Validator::make($this->request->post(), [
//            'user_id'   =>  'required|integer|exists:mini_user,uid',
            'name'      =>  'required|string',
            'nick_name' =>  'required|string',
            'phone'     =>  'required|regex:/^1[345789][0-9]{9}$/',
            'parent_id' =>  'integer|exists:mini_box_owner,id',
            'level'     =>  'required|integer|in:1,3,5',
//            'desc'      =>  'string'
        ], [
            'user_id.required'  =>  '用户ID不能为空',
            'user_id.integer'   =>  '用户ID必须为整型',
            'user_id.exists'    =>  '用户不存在',
            'name.required'     =>  '盒子名称不能为空',
            'name.string'       =>  '盒子名称必须为字符',
            'nick_name.required'=>  '用户昵称不能为空',
            'nick_name.string'  =>  '用户昵称必须为字符',
            'phone.required'    =>  '用户手机号不能为空',
            'phone.regex'       =>  '用户手机号错误',
            'parent_id.integer' =>  '上级用户必须整型',
            'parent_id.exists'  =>  '上级用户不存在',
            'level.required'    =>  '星级不能为空',
            'level.integer'     =>  '星级必须整型',
            'level.in'          =>  '星级选择错误',
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_OWNER_ADD_ERROR, $validator->errors()->first());
        }

        [$flag, $result]    =   $this->service->ownerAdd($this->request->post());
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_OWNER_ADD_ERROR, $result);
        }
    }

    /**
     * 编辑盒子用户
     * @return \Illuminate\Http\JsonResponse
     */
    public function editOwner()
    {
        $validator  =   Validator::make($this->request->post(), [
//            'user_id'   =>  'required|integer|exists:mini_user,uid',
            'name'      =>  'required|string',
            'nick_name' =>  'required|string',
            'phone'     =>  'required|regex:/^1[345789][0-9]{9}$/',
//            'parent_id' =>  'integer|exists:mini_box_owner,id',
//            'desc'      =>  'string',
            'owner_id'  =>  'required|integer|exists:mini_box_owner,id',
            'level'     =>  'required|integer|in:1,3,5',
        ], [
            'user_id.required'  =>  '用户ID不能为空',
            'user_id.integer'   =>  '用户ID必须为整型',
            'user_id.exists'    =>  '用户不存在',
            'name.required'     =>  '盒子名称不能为空',
            'name.string'       =>  '盒子名称必须为字符',
            'nick_name.required'=>  '用户昵称不能为空',
            'nick_name.string'  =>  '用户昵称必须为字符',
            'phone.required'    =>  '用户手机号不能为空',
            'phone.regex'       =>  '用户手机号错误',
            'parent_id.integer' =>  '上级用户必须整型',
            'parent_id.exists'  =>  '上级用户不存在',
            'desc.string'       =>  '盒子用户描述必须为字符',
            'owner_id.required' =>  '盒子用户ID不能为空',
            'owner_id.integer'  =>  '盒子用户ID必须为整型',
            'owner_id.exists'   =>  '盒子用户不存在',
            'level.required'    =>  '星级不能为空',
            'level.integer'     =>  '星级必须整型',
            'level.in'          =>  '星级选择错误',
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_OWNER_EDIT_ERROR, $validator->errors()->first());
        }

        $owner_id   =   (int) $this->request->post('owner_id');
        [$flag, $result]    =   $this->service->ownerEdit($this->request->post(), $owner_id);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_OWNER_EDIT_ERROR, $result);
        }
    }

    /**
     * 删除盒子用户
     * @return \Illuminate\Http\JsonResponse
     */
    public function deleteOwner()
    {
        $validator  =   Validator::make($this->request->post(), [
            'owner_id'  =>  'required|integer|exists:mini_box_owner,id'
        ], [
            'owner_id.required' =>  '盒子用户ID不能为空',
            'owner_id.integer'  =>  '盒子用户ID必须为整型',
            'owner_id.exists'   =>  '盒子用户不存在'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_OWNER_DELETE_ERROR, $validator->errors()->first());
        }

        $owner_id   =   (int) $this->request->post('owner_id');
        [$flag, $result]    =   $this->service->ownerDelete($owner_id);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_OWNER_DELETE_ERROR, $result);
        }
    }

    /**
     * 盒子用户信息
     * @return \Illuminate\Http\JsonResponse
     */
    public function infoOwner()
    {
        $validator  =   Validator::make($this->request->post(), [
            'owner_id'  =>  'required|integer|exists:mini_box_owner,id'
        ], [
            'owner_id.required' =>  '盒子用户ID不能为空',
            'owner_id.integer'  =>  '盒子用户ID必须为整型',
            'owner_id.exists'   =>  '盒子用户不存在'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_OWNER_DELETE_ERROR, $validator->errors()->first());
        }

        $owner_id   =   (int) $this->request->post('owner_id');
        [$flag, $result]    =   $this->service->ownerInfo($owner_id);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_OWNER_DELETE_ERROR, $result);
        }
    }
}
